package speakhunter;

import java.io.IOException;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class RegistroProfessor extends HttpServlet {

	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		boolean aulaOnline = true;
		String nome = req.getParameter("nome");
		String email = req.getParameter("email");
		String idiomas = req.getParameter("idiomas");
		String preco = req.getParameter("preco");
		String online = req.getParameter("aulaOnline");
		if (online.equals("Sim")) {
			aulaOnline = true;

		} else {
			aulaOnline = false;
		}

		int tempoExperiencia = Integer.parseInt(req
				.getParameter("tempoExperiencia"));

		String educacao = req.getParameter("educacao");
		String telefone = req.getParameter("telefone");
		String cidade = req.getParameter("cidade");
		String estado = req.getParameter("estado");
		String descricao = req.getParameter("descricao");

		// obtendo a imagem
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
		BlobKey blobKey = blobs.get("myFile");
		Professor professor = new Professor(nome, idiomas, preco, descricao,
				aulaOnline, tempoExperiencia, educacao, email, telefone,
				cidade, estado, blobKey);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(professor);
		} finally {
			pm.close();
		}

		req.setAttribute("blobkey", blobKey);
		RequestDispatcher view = req.getRequestDispatcher("ListarContatos.jsp");
		view.forward(req, resp);
		
	/*	if (blobKey == null) {
			resp.sendRedirect("obrigado.html");
		} else {
			resp.sendRedirect("ListarContatos.jsp");
		}*/
	}

}
